package Views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Controllers.PlayerController;


public class IHMPlayer extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JPanel panel;
	private PlayerController controller;
	private JButton playButton;
	private JButton stopButton;

	public  IHMPlayer(PlayerController controller){
		super();
		this.controller = controller;
		build();
	}

	/***
	 * Creer la fenetre
	 */
	private void build() {
		setTitle("MIDI player");
		setSize(400, 100);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setAlwaysOnTop(true);
		setContentPane(buildPanel());		
	}

	/***
	 * Creer le panel a afficher
	 * @return panel a afficher
	 */
	public JPanel buildPanel() {
		panel = new JPanel();

		playButton = new JButton(new ImageIcon("Resources/playbutton.png"));
		stopButton = new JButton(new ImageIcon("Resources/stopbutton.png"));

		playButton.addActionListener(this);
		stopButton.addActionListener(this);

		panel.add(playButton);
		panel.add(stopButton);

		return panel;
	}

	public JPanel getPanel() {
		return panel;
	}

	public void setPanel(JPanel panel) {
		this.panel = panel;
	}

	/**
	 * Permet de choisir le mode de lecture (Charger/Creer)
	 * @param table
	 * @return clef de la hashtable choisie par l'utilisateur
	 */
	public String chooseMode(Hashtable<String, String> table) {
		String retour, possibilities[] = new String[table.size()];
		int j = 0;

		//Ajout des modes de lecture dans la dropdown list
		for(String key : table.keySet()) { 
			possibilities[j] = key;
			j++;
		}

		retour = (String)JOptionPane.showInputDialog(
				this,
				"Choisir le mode de lecture",
				"Mode de lecture",
				JOptionPane.PLAIN_MESSAGE,
				null,
				possibilities,
				"Charger");

		return retour;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(playButton)) {
			controller.playClick();
		}
		else if(e.getSource().equals(stopButton)) {
			controller.stopClick();
		}
	}
}